#include "opendialog.h"

OpenDialog::OpenDialog(QWidget *parent)
    : QDialog(parent),
      portNameEdit(new QLineEdit(this)),
      baudRateEdit(new QLineEdit(this)),
      dataBitsEdit(new QLineEdit(this)),
      parityEdit(new QLineEdit(this)),
      stopBitsEdit(new QLineEdit(this)),
      flowControlEdit(new QLineEdit(this))
{
    setWindowTitle("Serial Port Configuration");
    setFixedSize(400, 300);
    // 移除问号按钮
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    QGridLayout *layout = new QGridLayout(this);

    layout->addWidget(new QLabel("Port Name:", this), 0, 0);
    layout->addWidget(portNameEdit, 0, 1);

    layout->addWidget(new QLabel("Baud Rate:", this), 1, 0);
    layout->addWidget(baudRateEdit, 1, 1);

    layout->addWidget(new QLabel("Data Bits:", this), 2, 0);
    layout->addWidget(dataBitsEdit, 2, 1);

    layout->addWidget(new QLabel("Parity:", this), 3, 0);
    layout->addWidget(parityEdit, 3, 1);

    layout->addWidget(new QLabel("Stop Bits:", this), 4, 0);
    layout->addWidget(stopBitsEdit, 4, 1);

    layout->addWidget(new QLabel("Flow Control:", this), 5, 0);
    layout->addWidget(flowControlEdit, 5, 1);

    setLayout(layout);
}

void OpenDialog::setValues(const SerialConfig &config)
{
    portNameEdit->setText(config.portName);
    baudRateEdit->setText(QString::number(config.baudRate));
    dataBitsEdit->setText(QString::number(config.dataBits));
    parityEdit->setText(QString::number(config.parity));
    stopBitsEdit->setText(QString::number(config.stopBits));
    flowControlEdit->setText(config.flowControl);
}
